home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / hdf3_2r2.lha / HDF3.2r2 / util / r8tohdf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  7.0 KB  |  273 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.4 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/util/RCS/r8tohdf.c,v 1.4 1992/08/06 14:27:57 mlivin beta koziol $
  30.  
  31. $Log: r8tohdf.c,v $
  32.  * Revision 1.4  1992/08/06  14:27:57  mlivin
  33.  * made it append all RIGs to file, not crush the given output file
  34.  *
  35.  * Revision 1.3  1992/07/15  21:48:48  sxu
  36.  *  Added changes for CONVEX
  37.  *
  38.  * Revision 1.2  1992/07/01  20:14:53  mlivin
  39.  * cleaned up some little things
  40.  *
  41.  * Revision 1.1  1992/06/30  20:43:55  mlivin
  42.  * Initial revision
  43.  *
  44.  * Revision 3.6  1991/10/22  17:56:10  dilg
  45.  * 5
  46.  * HDF3.1r5
  47.  *
  48.  * New machine types added:
  49.  *
  50.  *         PC      - IBM PC (DOS)
  51.  *         WIN     - IBM PC (Microsoft Windows 3.0)
  52.  *         IBM6000 - IBM RS/6000 (AIX)
  53.  *         CONVEX  - Convex C-2 (Unix)
  54.  *
  55.  * Bugs fixed in:
  56.  *
  57.  *         scup32.f
  58.  *         cspck32.f
  59.  *         dfpFf.f
  60.  *         dfpF.c
  61.  *         dfsd.c
  62.  *
  63.  * New utility added:
  64.  *
  65.  *         ristosds.c - convert raster images to sds.
  66.  *
  67.  * Also:
  68.  *         All code for the library was modified to conform to the
  69.  *         ANSI C standard.
  70.  *
  71.  * Revision 3.5  1990/07/02  10:11:30  clow
  72.  * some cosmetic modifications
  73.  *
  74. */
  75.  
  76. /*
  77. *  r8tohdf.c
  78. *  Encoding of raster images in HDF files
  79. */
  80.  
  81. /* The intrepretation of arguments has changed a little.  A -p introduces a
  82.    palette which will be used for subsequent images, till another -p.
  83.    -i and -c introduce a series of images/compressed images */
  84.  
  85. #include "hdf.h"
  86.  
  87. int32 xdim, ydim;
  88.  
  89. #ifdef PROTOTYPE
  90. int main(int argc, char *argv[]);
  91. int palconv(char *palfile);
  92. int imconv(char *outfile, char *imfile, uint16 compress);
  93. #else
  94. int main();
  95. int palconv();
  96. int imconv();
  97. #endif /* PROTOTYPE */
  98.  
  99. #ifdef PROTOTYPE
  100. main(int argc, char *argv[]) 
  101. #else
  102. main(argc,argv) 
  103.     int argc;
  104.     char *argv[];
  105. #endif /* PROTOTYPE */
  106. {
  107.     int i, is_pal = 0, image = 1;
  108.     char *outfile;
  109.     uint16 compress = (uint16) 0;
  110.  
  111.     if (argc < 5) { 
  112.         printf("%s,  version: 1.1   date: July 1, 1992\n", argv[0]);
  113.     printf("   This utility converts one or more raw raster-8 images to\n");
  114.     printf("   HDF RIS8 format and writes them to an HDF file.\n\n");
  115.         printf("Usage:\n");
  116.         printf("  %s xdim ydim outfile [-p palfile] ", argv[0]);
  117.         printf("{[-r],[-c],[-i]} imagefile\n");
  118.         printf("\t\t\t\t... [-p palfile] {[-r],[-c],[-i]} imagefile ...\n");
  119.         printf("  -r: Store without compression (default)\n");
  120.         printf("  -c: Store using RLE compression\n");
  121.         printf("  -i: Store using IMCOMP compression\n\n");
  122.         printf("* r8tohdf can take any number of images and palettes\n");
  123.         printf("* Compression, palette, apply to all subsequent images.\n");
  124.         printf("* All images are assumed to be the same dimensions.\n\n");
  125.         exit(1);
  126.     }
  127.  
  128.     xdim = atoi(argv[1]);
  129.     ydim = atoi(argv[2]);
  130.  
  131.     if (xdim < 1 || ydim < 1) {
  132.         printf("Must specify xdim and ydim\n");
  133.         exit(1);
  134.     }
  135.  
  136.     outfile = argv[3];
  137.  
  138.     for (i=4; i<argc; i++) {
  139.         if (*argv[i] == '-') {
  140.             switch (argv[i][1]) {
  141.                 case 'p':       /* palette */
  142.                     is_pal = 1;
  143.                     image = 0;
  144.                     break;
  145.                 case 'r':       /* raster */
  146.                     image = 1;
  147.                     compress = (uint16) 0;
  148.                     break;
  149.                 case 'c':       /* RLE */
  150.                     image = 1;
  151.                     compress = DFTAG_RLE;
  152.                     break;
  153.                 case 'i':       /* IMCOMP */
  154.                     image = 1;
  155.                     compress = DFTAG_IMC;
  156.                     break;
  157.                 default:
  158.                     printf("Illegal option: %s, skipping....\n", argv[i]);   
  159.                     break;
  160.             }
  161.         }
  162.         else { /* file name */
  163.             if (image) {
  164.                 if (compress == DFTAG_IMC && is_pal == 0) {
  165.                     printf("Illegal options.  If imcomp compression (-i) ");
  166.                     printf("chosen, you must supply a palette.\n");
  167.                     printf("Program aborted.\n");
  168.                     exit(1);
  169.                 }
  170.                 imconv(outfile, argv[i], compress);
  171.             } else {
  172.                 palconv(argv[i]);
  173.                 image = 1;
  174.             }
  175.         }
  176.       }
  177.     return(0);
  178. }
  179.  
  180. /*
  181.  *  palconv(file) sets the palette
  182.  */
  183.  
  184. #ifdef PROTOTYPE
  185. palconv(char *palfile)
  186. #else
  187. palconv(palfile)
  188. char *palfile;
  189. #endif /* PROTOTYPE */
  190. {
  191.     uint8 palspace[1024], reds[256], greens[256], blues[256];
  192.     uint8 *p;
  193.     FILE *fp;
  194.     int j,ret;
  195.  
  196. #ifdef PC
  197.     fp = fopen(palfile, "rb");
  198. #else
  199.     fp = fopen(palfile, "r");
  200. #endif
  201.     if (fp == NULL) {
  202.         printf(" Error opening palette file %s\n", palfile);
  203.         exit(1);
  204.     }
  205.     fread(reds, 1, 256, fp);
  206.     fread(greens, 1, 256, fp);
  207.     fread(blues, 1, 256, fp);
  208.     fclose(fp);
  209.  
  210.     p = palspace;
  211.     for (j=0; j<256; j++) {
  212.         *p++ = reds[j];
  213.         *p++ = greens[j];
  214.         *p++ = blues[j];
  215.     }
  216.  
  217.     ret = DFR8setpalette(palspace);
  218.     if (ret < 0) {
  219.         printf(" Error: %d, in writing palette %s\n", ret, palfile);
  220.         exit(1);
  221.     }
  222.     return(0);
  223. }
  224.  
  225.  
  226. #ifdef PROTOTYPE
  227. imconv(char *outfile, char *imfile, uint16 compress)
  228. #else
  229. imconv(outfile, imfile, compress)
  230. char *outfile;
  231. char *imfile;
  232. uint16 compress;
  233. #endif /* PROTOTYPE */
  234. {
  235.     int ret;
  236.     char *space;
  237.     FILE *fp;
  238.  
  239. #ifdef PC
  240.     if ((fp = fopen(imfile, "rb")) == NULL) {
  241.         printf("Error opening image file\n");
  242.         exit(1);
  243.     }
  244. #else
  245.     if ((fp = fopen(imfile, "r")) == NULL) {
  246.         printf("Error opening image file\n");
  247.         exit(1);
  248.     }
  249. #endif
  250.  
  251.     if ((space = (char *)HDgetspace((uint32) xdim * ydim)) == NULL) {
  252.         printf("Not enough memory to convert image\n");
  253.         exit(1);
  254.     }
  255.  
  256.     if (fread(space, (int) xdim, (int) ydim, fp) <= 0) {
  257.         printf("Cannot read image file\n");
  258.         fclose(fp);
  259.         exit(1);
  260.     }
  261.  
  262.     ret = DFR8addimage(outfile, space, xdim, ydim, compress);
  263.  
  264.     if (ret < 0) {
  265.         printf(" Error: %d, in writing image %s\n", HEvalue(1), imfile);
  266.         exit(1);
  267.     }
  268.  
  269.     HDfreespace(space);
  270.     fclose(fp);
  271.     return(0);
  272. }
  273.